package com.tsll.service.impl;

import com.tsll.dao.BookMapper;
import com.tsll.dao.BorrowBookMapper;
import com.tsll.entity.Book;
import com.tsll.entity.BorrowBook;
import com.tsll.service.BorrowService;
import com.tsll.vo.BorrowVo;
import com.tsll.vo.RebackVo;
import com.tsll.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {
    @Autowired
    private BorrowBookMapper borrowbookMapper;

    @Autowired
    private BookMapper bookMapper;
//    归还图书信息
    public Result getReturn() {
        return new Result(200,"获取归还图书信息成功",borrowbookMapper.return1());
    }
//    借出图书信息
    public Result getLend() {
        return new Result(200,"获取借出图书信息成功",borrowbookMapper.lend());
    }
//    借阅审核图书详情信息
    public Result lendByIdAndByUserid(Integer id, Integer userId) {
        return new Result(200,"获取审核详情信息成功",borrowbookMapper.lendDetails(id,userId));
    }
//    审核通过修改借阅状态
    public Result getAccess(Integer id) {
        return new Result(200,"借阅状态为已借出，审核状态为通过",borrowbookMapper.updateExam(id));
    }
//审核失败修改驳回信息
    public Result updateBack(RebackVo rebackVo) {
        return new Result(200,"驳回成功",borrowbookMapper.updateBack(rebackVo));
    }

    @Override
    public Result borrowBook(BorrowVo borrowVo) {
        if(borrowVo.getScore()>=80){
            borrowVo.setExaminestatus(1);
            borrowVo.setStatus(0);
            bookMapper.updateBookCount(borrowVo.getBookId(),1);
            borrowbookMapper.insertBorrowInfo(borrowVo);
            return new Result(200,"借阅成功",null);
        }
        borrowVo.setExaminestatus(0);
        borrowVo.setStatus(2);
        borrowVo.setReback("信誉分不足,无法借阅");
        borrowbookMapper.insertBorrowInfo(borrowVo);
        return new Result(200,"信誉分不足,无法借阅",1);
    }

    @Override
    public Result borrowBooks(Integer id) {
        List<BorrowBook> borrowBookInfo = borrowbookMapper.selectByUserIdBorrowBooks(id);
        List<Book> books=new ArrayList<>();
        for (BorrowBook borrowBook:borrowBookInfo) {
            Book book = bookMapper.selectById(borrowBook.getBookId());
            books.add(book);
        }

        return new Result(200,"查询借阅书单成功",books);

    }


}
